package com.cloud.rapid.screen.util;


import cn.hutool.core.bean.BeanUtil;

import java.util.ArrayList;
import java.util.Collection;

/**
 * bean工具类
 *
 * @author lvlinguang
 * @date 2020-04-29 17:46
 */
public class BeanUtils {


    private BeanUtils() {
    }

    /**
     * 对象转换
     *
     * @param data         数据对象
     * @param convertClass 要转换的类型
     * @param <T>
     * @return
     */
    public static <T> T convertProperties(Object data, Class<T> convertClass) {
        if (data == null) {
            return null;
        }
        try {
            T result = convertClass.newInstance();
            BeanUtil.copyProperties(data, result);
            return result;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 对象转换
     *
     * @param data         数据对象
     * @param convertClass 要转换的类型
     * @param <T>
     * @return
     */
    public static <T> T convertProperties(Object data, Class<T> convertClass, String... ignoreProperties) {
        if (data == null) {
            return null;
        }
        try {
            T result = convertClass.newInstance();
            BeanUtil.copyProperties(data, result, ignoreProperties);
            return result;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 集合转换
     *
     * @param dataList     list集合数据
     * @param convertClass 要转换的类型
     * @param <T>
     * @return
     */
    public static <T> ArrayList<T> convertProperties(Collection<?> dataList, Class<T> convertClass) {
        if (dataList == null) {
            return null;
        }
        try {
            ArrayList<T> resultData = new ArrayList<>();
            for (Object item : dataList) {
                T result = convertClass.newInstance();
                BeanUtil.copyProperties(item, result);
                resultData.add(result);
            }
            return resultData;
        } catch (Exception e) {
            return null;
        }
    }
}
